package zhoug.zui.input;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.UUID;

import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import zhoug.common.utils.AntiShakeHelper;
import zhoug.common.utils.FocusClickHelper;
import zhoug.common.utils.StringUtils;
import zhoug.zui.R;
import zhoug.zui.date.DateSelectInterface;
import zhoug.zui.dialog.date.ZUIMultiDateChooser;

/**
 * 开始时间和结束时间选择
 *
 * @Author: zhoug
 * @Date: 2024-07-19
 * @Description:
 */
public class ZUIInputMultiDateSelectView extends ZUIInputView implements ZUIMultiDateChooser.Callback {
    public static final int MODE_DATE = DateSelectInterface.MODE_DATE;//0001 年月日
    public static final int MODE_TIME = DateSelectInterface.MODE_TIME;//0010 时分
    public static final int MODE_DATE_TIME = DateSelectInterface.MODE_DATE_TIME;//0011 年月日 时分
    private TextView mTvStartTime;
    private TextView mTvEndTime;
    private TextView mTvDivider;
    private OnSelectDateListener onSelectDateListener;
    private String startHintText;
    private String endHintText;

    private String startTime;
    private String endTime;
    /**
     * {@link #MODE_DATE,#MODE_TIME,#MODE_DATE_TIME}
     */
    private int dateTimeMode = MODE_DATE;
    private boolean mDialogOverlay = true;//是否显示日期选择Dialog遮罩
    private boolean mShowYear = true;
    private boolean mShowMonth = true;
    private boolean mShowDay = true;
    private boolean mShowHour = true;
    private boolean mShowMinute = true;

    private AntiShakeHelper mAntiShakeHelper=new AntiShakeHelper();
    /**
     * 是否开启防抖,默认开启
     */
    private boolean mAntiShakeEnable=true;
    private static final String ANTISHAKE_EVENID= UUID.randomUUID().toString();


    public ZUIInputMultiDateSelectView(Context context) {
        this(context, null);
    }

    public ZUIInputMultiDateSelectView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.ZUIInputMultiDateSelectView_theme);
    }

    public ZUIInputMultiDateSelectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    protected void init(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ZUIInputMultiDateSelectView, defStyleAttr, R.style.ZUIInputMultiDateSelectView_def);
        startHintText = a.getString(R.styleable.ZUIInputMultiDateSelectView_zui_startHintText);
        endHintText = a.getString(R.styleable.ZUIInputMultiDateSelectView_zui_endHintText);
        String dividerText = a.getString(R.styleable.ZUIInputMultiDateSelectView_zui_dividerText);
        int hintColor = a.getColor(R.styleable.ZUIInputMultiDateSelectView_zui_hintColor, ContextCompat.getColor(context, R.color.zui_hintColor));
        int timeTextColor = a.getColor(R.styleable.ZUIInputMultiDateSelectView_zui_timeTextColor, ContextCompat.getColor(context, R.color.zui_textColor));
        float timeTextSize = a.getDimension(R.styleable.ZUIInputMultiDateSelectView_zui_timeTextSize, context.getResources().getDimension(R.dimen.zui_textSize));
        int dividerTextColor = a.getColor(R.styleable.ZUIInputMultiDateSelectView_zui_dividerTextColor, ContextCompat.getColor(context, R.color.zui_hintColor));
        float dividerTextSize = a.getDimension(R.styleable.ZUIInputMultiDateSelectView_zui_dividerTextSize, context.getResources().getDimension(R.dimen.zui_textSize));
        dateTimeMode = a.getInt(R.styleable.ZUIInputMultiDateSelectView_zui_dateTimeMode, MODE_DATE);
        a.recycle();


        setStartHintText(startHintText);
        setEndHintText(endHintText);
        setDividerText(dividerText);
        setHintTextColor(hintColor);
        setTimeTextColor(timeTextColor);
        setTimeTextSize(timeTextSize);
        setDividerTextColor(dividerTextColor);
        setDividerTextSize(dividerTextSize);

        mInputValueContainer.setClickable(true);
        mInputValueContainer.setFocusable(true);
        mInputValueContainer.setFocusableInTouchMode(true);
        FocusClickHelper.bind(mInputValueContainer);
        mInputValueContainer.setOnClickListener(v -> {
            if(mAntiShakeEnable){
                mAntiShakeHelper.execute(new AntiShakeHelper.Task() {
                    @Override
                    public String evenId() {
                        return ANTISHAKE_EVENID;
                    }

                    @Override
                    public void run() {
                        showTimeChooser(ZUIMultiDateChooser.TYPE_START);
                    }
                });
            }else{
                showTimeChooser(ZUIMultiDateChooser.TYPE_START);
            }

        });
        mInputValueContainer.setOnFocusChangeListener(this);

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    protected View createValueView(ViewGroup parent) {
        View root = LayoutInflater.from(getContext()).inflate(R.layout.zui_input_multi_date_selector, parent, false);
        mTvStartTime = root.findViewById(R.id.tv_startTime);
        mTvEndTime = root.findViewById(R.id.tv_endTime);
        mTvDivider = root.findViewById(R.id.tv_divider);
        return root;
    }

    @Override
    protected void onClearClick(View view) {
        mTvStartTime.setText("");
        mTvEndTime.setText("");
    }


    private void showTimeChooser(int type) {
        if (!isEnabled()) {
            return;
        }
        mRoot.requestFocus();
        ZUIMultiDateChooser dialog = new ZUIMultiDateChooser(getContext());
        dialog.setOverlay(mDialogOverlay);
        dialog.setDateMode(dateTimeMode);
        dialog.setStartTimeName(startHintText);
        dialog.setEndTimeName(endHintText);
        dialog.setStartTime(startTime);
        dialog.setEndTime(endTime);
        dialog.setCallback(this);
        dialog.showYear(mShowYear);
        dialog.showMonth(mShowMonth);
        dialog.showDay(mShowDay);
        dialog.showHour(mShowHour);
        dialog.showMinute(mShowMinute);
        dialog.setType(type);
        dialog.show();

    }

    public void showYear(boolean show){
        this.mShowYear= show;
    }
    public void showMonth(boolean show){
        this.mShowMonth= show;
    }

    public void showDay(boolean show){
        this.mShowDay= show;
    }

    public void showHour(boolean show){
        this.mShowHour= show;
    }

    public void showMinute(boolean show){
        this.mShowMinute= show;
    }

    public void setStartAndEndTime(String startTime,String endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
        mTvStartTime.setText(startTime);
        mTvEndTime.setText(endTime);
    }


    @Override
    public void onSelectDate(String startTime, String endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
        mTvStartTime.setText(startTime);
        mTvEndTime.setText(endTime);
        if (onSelectDateListener != null) {
            onSelectDateListener.onSelect(this, this.startTime, this.endTime);
        }
        if(isRequired() && isAutoValidateRequired()){
            setError(StringUtils.isNullOrEmpty(startTime)||StringUtils.isNullOrEmpty(endTime));
        }
    }

    public String getStartTime() {
        return startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setDialogOverlay(boolean dialogOverlay) {
        this.mDialogOverlay = dialogOverlay;
    }

    public void setStartHintText(String startHintText) {
        this.startHintText = startHintText;
        mTvStartTime.setHint(startHintText);
    }

    public void setEndHintText(String endHintText) {
        this.endHintText = endHintText;
        mTvEndTime.setHint(endHintText);
    }

    public void setHintTextColor(int color) {
        if (color != 0) {
            mTvStartTime.setHintTextColor(color);
            mTvEndTime.setHintTextColor(color);
        }
    }

    public void setTimeTextColor(int color) {
        if (color != 0) {
            mTvStartTime.setTextColor(color);
            mTvEndTime.setTextColor(color);
        }
    }

    public void setTimeTextSize(float size) {
        if (size > 0) {
            mTvStartTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
            mTvEndTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        }
    }


    public void setDividerTextColor(int color) {
        if (color != 0) {
            mTvDivider.setTextColor(color);
        }
    }

    public void setDividerTextSize(float size) {
        if (size > 0) {
            mTvDivider.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        }
    }

    public void setDividerText(String text) {
        mTvDivider.setText(text);
    }


    public void setOnSelectDateListener(OnSelectDateListener onSelectDateListener) {
        this.onSelectDateListener = onSelectDateListener;
    }
    /**
     * 是否开启防抖,默认开启
     * @param antiShakeEnable
     */
    public void setAntiShakeEnable(boolean antiShakeEnable){
        this.mAntiShakeEnable=antiShakeEnable;
    }

    /**
     * 选择时间监听器
     */
    public interface OnSelectDateListener {
        void onSelect(ZUIInputMultiDateSelectView view, String startTime, String endTime);
    }


}
